package com.hna.face.controller.business;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hna.face.base.BaseController;
import com.hna.face.common.result.BaseResult;
import com.hna.face.model.FaceRecognizeResult;
import com.hna.face.model.PageInfo;
import com.hna.face.model.RecognizeResultAndScheduleVo;
import com.hna.face.model.RecognizeResultVo;
import com.hna.face.service.IFaceRecognizeResultService;


@Controller
public class RecognizeController extends BaseController {

	@Autowired 
	IFaceRecognizeResultService recognizeResultService;

    
    private final static Logger log = LoggerFactory.getLogger(RecognizeController.class);
    

    @RequestMapping("/recognize/index")
    public String index() {
        return "pages/recognize/index";
    }
    
    
    @RequestMapping("/recognize/add")
    public String add() {
        return "pages/recognize/add";
    }
    
  
    

//    @RequestMapping("/recognize/detail")
//    public ModelAndView details(HttpServletRequest request) {
//        ModelAndView modelAndView = new ModelAndView("pages/recognize/detail");
//        modelAndView.addObject("id", request.getParameter("id"));
//        return modelAndView;
//    }
//    
//    
//    @RequestMapping("/recognize/edit")
//	public ModelAndView edit(HttpServletRequest request) {
//		ModelAndView modelAndView = new ModelAndView("pages/recognize/edit");
//		modelAndView.addObject("id", request.getParameter("id"));
//		return modelAndView;
//	}

   
    
    @RequestMapping(value = { "/rest/recognize/list" }, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request, PageInfo pageInfo,FaceRecognizeResult recognize) {
        Map<String, Object> res = new HashMap<>();
        try {
//            Wrapper<RecognizeResult> wrapper=new EntityWrapper<>();
//            if(StringUtils.isNotBlank(recognize.getName())){
//        		wrapper.eq("name", recognize.getName());
//            }
//            if(StringUtils.isNotBlank(recognize.getMasterName())){
//        		wrapper.like("master_name", recognize.getMasterName());
//            }
//            Page<RecognizeResult> page = new Page<>(pageInfo.getPage(),pageInfo.getRows());
//            Page<RecognizeResult> recognizeList = recognizeResultService.selectPage(page, wrapper);
//        	Map<String, Object> param=new HashMap<>();
//        	if(recognize.getName() == null) {
//        		param.put("name", "");
//        	}
//        	else {
//        		param.put("name", recognize.getName());
//        	}
//        	if(recognize.getScheduleOn() == null) {
//        		param.put("name", "");
//        	}
//        	else {
//            	param.put("scheduleOn", recognize.getScheduleOn());
//
//        	}
//        	if(recognize.getStartDate() == null) {
//        		param.put("startDate", "");
//        	}
//        	else {
//            	param.put("startDate", recognize.getStartDate());
//
//        	}
//        	
//        	if(recognize.getEndDate() == null) {
//        		param.put("endDate", "");
//        	}
//        	else {
//            	param.put("endDate", recognize.getEndDate());
//
//
//        	}
//        	Page<RecognizeResultAndScheduleVo> page=new Page<RecognizeResultAndScheduleVo>(pageInfo.getPage(),pageInfo.getRows());
//    		Page<RecognizeResultAndScheduleVo> list = recognizeResultService.selectByPage(page,param);
            Wrapper<FaceRecognizeResult> wrapper=new EntityWrapper<>();
            if(StringUtils.isNotBlank(recognize.getScheduleNo())){
        		wrapper.like("schedule_no", recognize.getScheduleNo());
            }
            if(StringUtils.isNotBlank(recognize.getVehicleNo())){
        		wrapper.like("vehicle_no", recognize.getVehicleNo());
            }
            if(recognize.getResult()!=null && recognize.getResult()!=0 ){
        		wrapper.eq("result", recognize.getResult());
            }
//            if(StringUtils.isNotBlank(vehicle.getMasterName())){
//        		wrapper.like("master_name", vehicle.getMasterName());
//            }
            
            wrapper.orderBy("create_time", false);
            Page<FaceRecognizeResult> page = new Page<>(pageInfo.getPage(),pageInfo.getRows());
            Page<FaceRecognizeResult> list = recognizeResultService.selectPage(page, wrapper);
            
//            res.put("rows", list.getRecords());
//            res.put("total", list.getTotal());
//            res.put("success", "true");
//            return res;
            
        	
    		res.put("rows", list.getRecords());
			res.put("total", list.getTotal());	
			res.put("success", "true");
			
        	//获取关联数据及总条数
//        	recognizeResultService.selectAll(recognize,pageInfo,res);
//            res.put("success", "true");
            return res;
        } catch (Exception e) {
        	e.printStackTrace();
            res.put("success", "false");
            return res;
        }
    }

    

    @RequestMapping("/rest/recognize/info")
    public @ResponseBody
    BaseResult info(HttpServletRequest request) {
            Long id=Long.parseLong( request.getParameter("id"));
            FaceRecognizeResult recognize = recognizeResultService.selectById(id);
            return BaseResult.success(recognize);
    }
    
    @RequestMapping(value = { "/rest/recognize/del" }, method = RequestMethod.POST)
    public @ResponseBody
    BaseResult del(HttpServletRequest request,String ids) {
        try {

        	if(StringUtils.isEmpty(ids)){
        		return BaseResult.success();
        	}
        	recognizeResultService.deleteBatchIds(Arrays.asList(ids.split(",")));
             return BaseResult.success();
        } catch (Exception e) {
        	log.error("发生异常了",e);
            return BaseResult.fail(e.getMessage());
        }
    }
    
   
    
    @RequestMapping(value = { "/rest/recognize/save" }, method = RequestMethod.POST)
    public @ResponseBody
    BaseResult save(HttpServletRequest request,HttpServletResponse response, String name) {
        Map<String, Object> res = new HashMap<>();
        try {
        	
            
        	FaceRecognizeResult recognize = new FaceRecognizeResult();
//        	recognize.setName(name);
        	Date now = new Date();
        	recognize.setCreateTime(now);
        	recognize.setUpdateTime(now);
        	recognizeResultService.insert(recognize);
            
        	return BaseResult.success();
        } catch (Exception e) {
        	log.error("发生异常了",e);
            return BaseResult.fail(e.getMessage());
        }
    }
    
    
    @RequestMapping(value = { "/rest/recognize/update" }, method = RequestMethod.POST)
    public @ResponseBody
    BaseResult update(HttpServletRequest request, FaceRecognizeResult recognize,String meterNo) {
        Map<String, Object> res = new HashMap<>();
        try {
//        	recognize.setMeterNo(meterNo);
        	recognizeResultService.updateById(recognize);
        	
//        	MjMeter meter = new MjMeter();
//        	meter.setrecognizeId(recognize.getId());
//        	meter.setInstallTime(recognize.getRegisteredTime());
//        	meter.setMeterId(meterId);
//			meterService.insert(meter );
            
        	return BaseResult.success();
        } catch (Exception e) {
        	log.error("发生异常了",e);
            return BaseResult.fail(e.getMessage());
        }
    }

  
}



 